package com.yuxl.common.auth.repository;

import com.yuxl.common.auth.entity.AuthUser;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import com.yuxl.common.core.annotation.PostgreSqlRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


/**
 * @author yuxl
 */
@PostgreSqlRepository
public interface AuthUserRepository extends ReactiveCrudRepository<AuthUser, Long> {

    @Query("""
            SELECT count(*) FROM auth_user
            WHERE 1 = 1
            AND (:#{#entity.nickname} IS NULL OR nickname LIKE CONCAT('%', :#{#entity.nickname}, '%'))
            AND (:#{#entity.username} IS NULL OR username LIKE CONCAT('%', :#{#entity.username}, '%'))
            """)
    Mono<Integer> count(AuthUser entity);

    @Query("""
            SELECT * FROM auth_user
            WHERE 1=1
            AND (:#{#entity.nickname} IS NULL OR nickname LIKE CONCAT('%', :#{#entity.nickname}, '%'))
            AND (:#{#entity.username} IS NULL OR username LIKE CONCAT('%', :#{#entity.username}, '%'))
            ORDER BY
                CASE WHEN :#{#entity.prop} IS NOT NULL AND :#{#entity.order} = 'ascending' THEN
                    CASE :#{#entity.prop}
                         WHEN 'createTime' THEN create_time
                         WHEN 'updateTime' THEN update_time
                         ELSE NULL
                    END
                END ASC,
                CASE WHEN :#{#entity.prop} IS NOT NULL AND :#{#entity.order} = 'descending' THEN
                    CASE :#{#entity.prop}
                         WHEN 'createTime' THEN create_time
                         WHEN 'updateTime' THEN update_time
                         ELSE NULL
                    END
                END DESC,
                CASE WHEN :#{#entity.prop} IS NULL THEN create_time END DESC
            LIMIT :limit OFFSET :offset
            """)
    Flux<AuthUser> page(AuthUser entity, long limit, long offset);

    @Query("""
            SELECT * FROM auth_user
            WHERE 1=1
            AND (:#{#entity.nickname} IS NULL OR nickname LIKE CONCAT('%', :#{#entity.nickname}, '%'))
            AND (:#{#entity.username} IS NULL OR username LIKE CONCAT('%', :#{#entity.username}, '%'))
            """)
    Flux<AuthUser> list(AuthUser entity);


    // 添加根据id 的 update语句
    @Query("""
            UPDATE auth_user
            SET nickname = :#{#entity.nickname},
                username = :#{#entity.username},
                avatar = :#{#entity.avatar},
                update_time = :#{#entity.updateTime}
            WHERE id = :#{#entity.id}
            RETURNING *
            """)
    Mono<AuthUser> updateById(AuthUser entity);

    @Query("""
            UPDATE auth_user
            SET password = :#{#entity.password}
            WHERE id = :#{#entity.id}
            """)
    Mono<Integer> updatePassword(AuthUser entity);

    Mono<AuthUser> findByUsername(String username);


}